{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d5248e60-c294-4be3-9b24-8dae99b4bf3b",
   "metadata": {
    "tags": []
   },
   "source": [
    "# 数据清理和数据预处理"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fef7c818-6be3-4d13-9c9f-8b05b4e8ff42",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 导入包"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7444ef8d-eccc-4ee1-a344-1253347c3973",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "22be631b-6dc1-42b1-a168-847708dab472",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 创建一个Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "508efe4c",
   "metadata": {},
   "outputs": [],
   "source": [
    "float_data = pd.Series([1.2, -3.5, np.nan, 0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ac84fd71",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    1.2\n",
       "1   -3.5\n",
       "2    NaN\n",
       "3    0.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float_data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "61a5c209-10a4-42d2-a060-65faf796470e",
   "metadata": {
    "tags": []
   },
   "source": [
    "## 查看该seies中是否有值为空"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "acbb93ff",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    False\n",
       "1    False\n",
       "2     True\n",
       "3    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float_data.isna()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "606ab2b4-fdb9-43a1-b469-97bb452c9b43",
   "metadata": {},
   "source": [
    "## 打开movies.dat文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cf4a814b-d077-4451-8529-1cd3f714b626",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_id</th>\n",
       "      <th>title</th>\n",
       "      <th>genres</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Toy Story (1995)</td>\n",
       "      <td>Animation|Children's|Comedy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>Adventure|Children's|Fantasy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Grumpier Old Men (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Waiting to Exhale (1995)</td>\n",
       "      <td>Comedy|Drama</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Father of the Bride Part II (1995)</td>\n",
       "      <td>Comedy</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Heat (1995)</td>\n",
       "      <td>Action|Crime|Thriller</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Sabrina (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>Tom and Huck (1995)</td>\n",
       "      <td>Adventure|Children's</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>Sudden Death (1995)</td>\n",
       "      <td>Action</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>GoldenEye (1995)</td>\n",
       "      <td>Action|Adventure|Thriller</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   movie_id                               title                        genres\n",
       "0         1                    Toy Story (1995)   Animation|Children's|Comedy\n",
       "1         2                      Jumanji (1995)  Adventure|Children's|Fantasy\n",
       "2         3             Grumpier Old Men (1995)                Comedy|Romance\n",
       "3         4            Waiting to Exhale (1995)                  Comedy|Drama\n",
       "4         5  Father of the Bride Part II (1995)                        Comedy\n",
       "5         6                         Heat (1995)         Action|Crime|Thriller\n",
       "6         7                      Sabrina (1995)                Comedy|Romance\n",
       "7         8                 Tom and Huck (1995)          Adventure|Children's\n",
       "8         9                 Sudden Death (1995)                        Action\n",
       "9        10                    GoldenEye (1995)     Action|Adventure|Thriller"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mnames = [\"movie_id\", \"title\", \"genres\"]\n",
    "movies = pd.read_table(\"datasets/movielens/movies.dat\", sep=\"::\", header=None, names=mnames, engine=\"python\")\n",
    "movies[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03697acc-f2f2-412c-8cdb-d7da306d79d3",
   "metadata": {},
   "source": [
    "## 选取genres行，用|作为分隔符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ca73fd29-ad1a-41bf-afe4-e9c3b5f05914",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Action</th>\n",
       "      <th>Adventure</th>\n",
       "      <th>Animation</th>\n",
       "      <th>Children's</th>\n",
       "      <th>Comedy</th>\n",
       "      <th>Crime</th>\n",
       "      <th>Documentary</th>\n",
       "      <th>Drama</th>\n",
       "      <th>Fantasy</th>\n",
       "      <th>Film-Noir</th>\n",
       "      <th>Horror</th>\n",
       "      <th>Musical</th>\n",
       "      <th>Mystery</th>\n",
       "      <th>Romance</th>\n",
       "      <th>Sci-Fi</th>\n",
       "      <th>Thriller</th>\n",
       "      <th>War</th>\n",
       "      <th>Western</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Action  Adventure  Animation  Children's  Comedy  Crime  Documentary  \\\n",
       "0       0          0          1           1       1      0            0   \n",
       "1       0          1          0           1       0      0            0   \n",
       "2       0          0          0           0       1      0            0   \n",
       "3       0          0          0           0       1      0            0   \n",
       "4       0          0          0           0       1      0            0   \n",
       "5       1          0          0           0       0      1            0   \n",
       "6       0          0          0           0       1      0            0   \n",
       "7       0          1          0           1       0      0            0   \n",
       "8       1          0          0           0       0      0            0   \n",
       "9       1          1          0           0       0      0            0   \n",
       "\n",
       "   Drama  Fantasy  Film-Noir  Horror  Musical  Mystery  Romance  Sci-Fi  \\\n",
       "0      0        0          0       0        0        0        0       0   \n",
       "1      0        1          0       0        0        0        0       0   \n",
       "2      0        0          0       0        0        0        1       0   \n",
       "3      1        0          0       0        0        0        0       0   \n",
       "4      0        0          0       0        0        0        0       0   \n",
       "5      0        0          0       0        0        0        0       0   \n",
       "6      0        0          0       0        0        0        1       0   \n",
       "7      0        0          0       0        0        0        0       0   \n",
       "8      0        0          0       0        0        0        0       0   \n",
       "9      0        0          0       0        0        0        0       0   \n",
       "\n",
       "   Thriller  War  Western  \n",
       "0         0    0        0  \n",
       "1         0    0        0  \n",
       "2         0    0        0  \n",
       "3         0    0        0  \n",
       "4         0    0        0  \n",
       "5         1    0        0  \n",
       "6         0    0        0  \n",
       "7         0    0        0  \n",
       "8         0    0        0  \n",
       "9         1    0        0  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dummies = movies[\"genres\"].str.get_dummies(\"|\")\n",
    "dummies[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc3c3c04-e797-4977-bdd5-17d41be3e4d5",
   "metadata": {},
   "source": [
    "## 加上前缀"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "71ed02b8-106b-4203-b58b-b8284d5db498",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>movie_id</th>\n",
       "      <th>title</th>\n",
       "      <th>genres</th>\n",
       "      <th>Genre_Action</th>\n",
       "      <th>Genre_Adventure</th>\n",
       "      <th>Genre_Animation</th>\n",
       "      <th>Genre_Children's</th>\n",
       "      <th>Genre_Comedy</th>\n",
       "      <th>Genre_Crime</th>\n",
       "      <th>Genre_Documentary</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>Toy Story (1995)</td>\n",
       "      <td>Animation|Children's|Comedy</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>Jumanji (1995)</td>\n",
       "      <td>Adventure|Children's|Fantasy</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>Grumpier Old Men (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>Waiting to Exhale (1995)</td>\n",
       "      <td>Comedy|Drama</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>Father of the Bride Part II (1995)</td>\n",
       "      <td>Comedy</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>Heat (1995)</td>\n",
       "      <td>Action|Crime|Thriller</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>Sabrina (1995)</td>\n",
       "      <td>Comedy|Romance</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>Tom and Huck (1995)</td>\n",
       "      <td>Adventure|Children's</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>9</td>\n",
       "      <td>Sudden Death (1995)</td>\n",
       "      <td>Action</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>10</td>\n",
       "      <td>GoldenEye (1995)</td>\n",
       "      <td>Action|Adventure|Thriller</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   movie_id                               title                        genres  \\\n",
       "0         1                    Toy Story (1995)   Animation|Children's|Comedy   \n",
       "1         2                      Jumanji (1995)  Adventure|Children's|Fantasy   \n",
       "2         3             Grumpier Old Men (1995)                Comedy|Romance   \n",
       "3         4            Waiting to Exhale (1995)                  Comedy|Drama   \n",
       "4         5  Father of the Bride Part II (1995)                        Comedy   \n",
       "5         6                         Heat (1995)         Action|Crime|Thriller   \n",
       "6         7                      Sabrina (1995)                Comedy|Romance   \n",
       "7         8                 Tom and Huck (1995)          Adventure|Children's   \n",
       "8         9                 Sudden Death (1995)                        Action   \n",
       "9        10                    GoldenEye (1995)     Action|Adventure|Thriller   \n",
       "\n",
       "   Genre_Action  Genre_Adventure  Genre_Animation  Genre_Children's  \\\n",
       "0             0                0                1                 1   \n",
       "1             0                1                0                 1   \n",
       "2             0                0                0                 0   \n",
       "3             0                0                0                 0   \n",
       "4             0                0                0                 0   \n",
       "5             1                0                0                 0   \n",
       "6             0                0                0                 0   \n",
       "7             0                1                0                 1   \n",
       "8             1                0                0                 0   \n",
       "9             1                1                0                 0   \n",
       "\n",
       "   Genre_Comedy  Genre_Crime  Genre_Documentary  \n",
       "0             1            0                  0  \n",
       "1             0            0                  0  \n",
       "2             1            0                  0  \n",
       "3             1            0                  0  \n",
       "4             1            0                  0  \n",
       "5             0            1                  0  \n",
       "6             1            0                  0  \n",
       "7             0            0                  0  \n",
       "8             0            0                  0  \n",
       "9             0            0                  0  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movies_windic = movies.join(dummies.add_prefix(\"Genre_\"))\n",
    "movies_windic.iloc[:10, :10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "2d2638e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>(0.0, 0.2]</th>\n",
       "      <th>(0.2, 0.4]</th>\n",
       "      <th>(0.4, 0.6]</th>\n",
       "      <th>(0.6, 0.8]</th>\n",
       "      <th>(0.8, 1.0]</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   (0.0, 0.2]  (0.2, 0.4]  (0.4, 0.6]  (0.6, 0.8]  (0.8, 1.0]\n",
       "0           0           0           0           0           1\n",
       "1           0           1           0           0           0\n",
       "2           1           0           0           0           0\n",
       "3           0           1           0           0           0\n",
       "4           0           0           1           0           0\n",
       "5           0           0           1           0           0\n",
       "6           0           0           0           0           1\n",
       "7           0           0           0           1           0\n",
       "8           0           0           0           1           0\n",
       "9           0           0           0           1           0"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(12345)\n",
    "value = np.random.uniform(size=10)\n",
    "bins = [0, 0.2, 0.4, 0.6, 0.8, 1]\n",
    "pd.get_dummies(pd.cut(value, bins))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b90f14ca-6d26-48c2-bd59-711d71b718d7",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 字符串操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c759af2e-502f-4ca6-ae5f-e74525e4e686",
   "metadata": {},
   "source": [
    "### build-in split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "749038bf-5ee9-4bf6-98e4-f212c95a94cd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', ' guido']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val = \"a,b, guido\"\n",
    "val.split(\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "874b361c-b444-4592-a2a7-917757374a78",
   "metadata": {},
   "source": [
    "### combine strip"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "98812e97-43be-403e-9ef8-91d264db7f30",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'b', 'guido']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pieces = [x.strip() for x in val.split(\",\")]\n",
    "pieces"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c1deb6b2-6aca-42e4-8d3f-c7073d3dc527",
   "metadata": {},
   "source": [
    "### 串联字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5788e135-9097-409f-ba4e-6c70aa65d437",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a::b::guido'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"::\".join(pieces)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3a534988-7bfd-4ebd-8cd3-76aa71bdae48",
   "metadata": {},
   "source": [
    "### 定位字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a67682ce-bd82-4a3a-bcc9-9af18deab4f5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "1\n",
      "1\n",
      "-1\n"
     ]
    }
   ],
   "source": [
    "print(\"guido\" in val) # in 方法\n",
    "print(val.index(\",\")) # index 方法\n",
    "print(val.find(\",\")) # find 方法\n",
    "print(val.find(\":\")) # find 方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68c9033e-d108-4f6c-9489-0d076f80dbd3",
   "metadata": {},
   "source": [
    "find函数找不到字符串时会返回-1, index找不到会报错"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "83187b89-dd90-4f0a-837e-fb0a081f3625",
   "metadata": {},
   "source": [
    "### 替换字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b258be0a-164a-48ac-a961-60be5cebaa0b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ab guido'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val.replace(\",\", \"::\").replace(\"::\", \"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "573711d9-8088-424a-8bab-4f631fc0b725",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 正则表达式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "0f84e10e-3de8-4752-b804-8a0d846886a2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4a4833a2-29e6-4643-823b-0ba5695fab7a",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 匹配空白字符\"\\s+\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "e62d29f1-2805-436a-841a-c004073965ae",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['foo', 'bar', 'baz', 'qux']"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"foo    bar\\t baz  \\tqux\"\n",
    "re.split(r\"\\s+\", text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "553c3f6f-1f03-491e-add5-2602659d0aea",
   "metadata": {},
   "source": [
    "### 编译regex，形成regex对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "dd24eec4-df04-408f-98d4-8fa33599a1bd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['foo', 'bar', 'baz', 'qux']"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regex = re.compile(r\"\\s+\")\n",
    "regex.split(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4e455352-c921-42ad-ba06-6b7683d5b5c2",
   "metadata": {},
   "source": [
    "### findall列出所有匹配的串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "92234657-f720-476e-8098-1e656d48d3ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['    ', '\\t ', '  \\t']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regex.findall(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56229a1a-9b02-492e-9258-d356ca683f69",
   "metadata": {},
   "source": [
    "### 正则表达式匹配邮箱"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "43bf6951-73e8-4859-9fc6-e63ca97323d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Dave@google.com', 'steve@gmail.com', 'rob@gmail.com', 'ryan@yahoo.com']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text = \"\"\"Dave Dave@google.com\n",
    "Steve steve@gmail.com\n",
    "Rob rob@gmail.com\n",
    "Ryan ryan@yahoo.com\"\"\"\n",
    "pattern = r\"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\"\n",
    "regex = re.compile(pattern, flags=re.I)\n",
    "\n",
    "regex.findall(text)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "77c8d6e3-db2e-425c-af6a-9168f84c34f4",
   "metadata": {
    "tags": []
   },
   "source": [
    "### search函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "de8c2187-ecd6-431e-97b9-93a5cc678531",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<re.Match object; span=(5, 20), match='Dave@google.com'>\n",
      "Dave@google.com\n",
      "Dave@google.com\n"
     ]
    }
   ],
   "source": [
    "# search只匹配第一个\n",
    "m = regex.search(text)\n",
    "print(m)\n",
    "print(text[m.start():m.end()])\n",
    "print(text[5:20])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8663aefb-e087-4de0-af21-47811a102c6e",
   "metadata": {},
   "source": [
    "### match函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "c72a86af-9aa4-4abd-9149-4ade9d493df8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "None\n"
     ]
    }
   ],
   "source": [
    "# match只匹配开头\n",
    "print(regex.match(text))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a206072-43cc-4822-bb30-4b7677ec6c5a",
   "metadata": {},
   "source": [
    "### sub函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b99735af-3690-4eb8-9326-2a90f3d2e2d1",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dave REDACTED\n",
      "Steve REDACTED\n",
      "Rob REDACTED\n",
      "Ryan REDACTED\n"
     ]
    }
   ],
   "source": [
    "print(regex.sub(\"REDACTED\", text))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "459760f8-19a0-4481-8a34-e26e917873f8",
   "metadata": {},
   "source": [
    "### 修改匹配模式将邮箱地址分为三个组件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "7dbd3d3e-3522-4574-865e-82cc0df5a337",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# 该修改过的regex可以返回一个模式组件的元组及其 groups方法： \n",
    "pattern = r\"([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})\"\n",
    "regex = re.compile(pattern, flags=re.IGNORECASE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "7394eab3-2d23-4a7b-9eeb-1cf048b50b3c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('wesm', 'bright', 'net')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "m = regex.match(\"wesm@bright.net\")\n",
    "m.groups()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e0b40d11-7ed4-413c-9f06-9b3ae2c81c1f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('Dave', 'google', 'com'),\n",
       " ('steve', 'gmail', 'com'),\n",
       " ('rob', 'gmail', 'com'),\n",
       " ('ryan', 'yahoo', 'com')]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "regex.findall(text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "519f286d-b21b-44fb-8a53-278b6211c79e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dave Username: Dave, Domain: google, suffix: com\n",
      "Steve Username: steve, Domain: gmail, suffix: com\n",
      "Rob Username: rob, Domain: gmail, suffix: com\n",
      "Ryan Username: ryan, Domain: yahoo, suffix: com\n"
     ]
    }
   ],
   "source": [
    "print(regex.sub(r\"Username: \\1, Domain: \\2, suffix: \\3\", text))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f96504e-56af-4fab-9f02-1a0d6f7e2acf",
   "metadata": {
    "tags": [],
    "toc-hr-collapsed": true
   },
   "source": [
    "## 字符串函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3e1660ec-dd8b-4c12-87b3-4aa2ae392884",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     False\n",
       "Steve    False\n",
       "Rob      False\n",
       "Wes       True\n",
       "dtype: bool"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 给出一个有缺失数据的Series\n",
    "data = {\"Dave\": \"dave@google.com\",\n",
    "         \"Steve\": \"steve@gmail.com\",\n",
    "         \"Rob\": \"rob@gmail.com\",\n",
    "         \"Wes\": np.nan}\n",
    "data = pd.Series(data)\n",
    "# 判断data中的数据是否为空\n",
    "data.isna()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0412eac1-de02-407b-be4f-9a73ef9d60cc",
   "metadata": {},
   "source": [
    "### str.contains 按行判断是否有该字符串"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "7384e287-aec2-443d-8d3f-9bd08e964903",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     False\n",
       "Steve     True\n",
       "Rob       True\n",
       "Wes        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断data中的数据是否有\"gmail\"\n",
    "data.str.contains(\"gmail\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "6f2f41b7-6177-42c6-b75f-cc144326b3ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     dave@google.com\n",
       "Steve    steve@gmail.com\n",
       "Rob        rob@gmail.com\n",
       "Wes                 <NA>\n",
       "dtype: string"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_as_string_ext = data.astype(\"string\")\n",
    "data_as_string_ext"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5359823a-4517-46a7-9a0e-c23f1c5f82d7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     False\n",
       "Steve     True\n",
       "Rob       True\n",
       "Wes       <NA>\n",
       "dtype: boolean"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_as_string_ext.str.contains(\"gmail\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "061cd5eb-fb6c-4fbe-a203-90a50344a263",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "pattern = r\"([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\\.([A-Z]{2,4})\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "22610eff-406f-4f3d-8a09-b3c5c14f924e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     [(dave, google, com)]\n",
       "Steve    [(steve, gmail, com)]\n",
       "Rob        [(rob, gmail, com)]\n",
       "Wes                        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.str.findall(pattern, flags=re.IGNORECASE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "c1ac1967-ee4d-4266-8d33-0cc9167749ef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     (dave, google, com)\n",
       "Steve    (steve, gmail, com)\n",
       "Rob        (rob, gmail, com)\n",
       "Wes                      NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.str.findall(pattern, flags=re.IGNORECASE).str[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "71f0bcf2-9325-442c-b71f-320816d9c125",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     google\n",
       "Steve     gmail\n",
       "Rob       gmail\n",
       "Wes         NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matches = data.str.findall(pattern, flags=re.IGNORECASE).str[0]\n",
    "matches.str.get(1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "154dbc1b-9bdd-41c5-92e9-f63fa47fb3f5",
   "metadata": {},
   "source": [
    "### str[:]按行切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "db0da682-76d7-4ff2-9208-10aa84cc31a5",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Dave     dave@\n",
       "Steve    steve\n",
       "Rob      rob@g\n",
       "Wes        NaN\n",
       "dtype: object"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按行切片\n",
    "data.str[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "9dcb9490-dcc6-4432-9520-b277bddc380e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Dave</th>\n",
       "      <td>dave</td>\n",
       "      <td>google</td>\n",
       "      <td>com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Steve</th>\n",
       "      <td>steve</td>\n",
       "      <td>gmail</td>\n",
       "      <td>com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rob</th>\n",
       "      <td>rob</td>\n",
       "      <td>gmail</td>\n",
       "      <td>com</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Wes</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0       1    2\n",
       "Dave    dave  google  com\n",
       "Steve  steve   gmail  com\n",
       "Rob      rob   gmail  com\n",
       "Wes      NaN     NaN  NaN"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.str.extract(pattern, flags=re.IGNORECASE)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "95bce063-bf07-4cdd-a3c3-dafbf4b7b418",
   "metadata": {},
   "source": [
    "## categorical extension type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "e6ed8eed-2321-4c51-8df1-e89ab3882d1b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>basket_id</th>\n",
       "      <th>fruit</th>\n",
       "      <th>count</th>\n",
       "      <th>weight</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>apple</td>\n",
       "      <td>11</td>\n",
       "      <td>1.564438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>orange</td>\n",
       "      <td>5</td>\n",
       "      <td>1.331256</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2</td>\n",
       "      <td>apple</td>\n",
       "      <td>12</td>\n",
       "      <td>2.393235</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>3</td>\n",
       "      <td>apple</td>\n",
       "      <td>6</td>\n",
       "      <td>0.746937</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>apple</td>\n",
       "      <td>5</td>\n",
       "      <td>2.691024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>5</td>\n",
       "      <td>orange</td>\n",
       "      <td>12</td>\n",
       "      <td>3.767211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>6</td>\n",
       "      <td>apple</td>\n",
       "      <td>10</td>\n",
       "      <td>0.992983</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>7</td>\n",
       "      <td>apple</td>\n",
       "      <td>11</td>\n",
       "      <td>3.795525</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   basket_id   fruit  count    weight\n",
       "0          0   apple     11  1.564438\n",
       "1          1  orange      5  1.331256\n",
       "2          2   apple     12  2.393235\n",
       "3          3   apple      6  0.746937\n",
       "4          4   apple      5  2.691024\n",
       "5          5  orange     12  3.767211\n",
       "6          6   apple     10  0.992983\n",
       "7          7   apple     11  3.795525"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fruits = ['apple', 'orange', 'apple', 'apple'] * 2\n",
    "N = len(fruits)\n",
    "rng = np.random.default_rng(seed=12345)\n",
    "df = pd.DataFrame({'fruit': fruits,\n",
    "                   'basket_id': np.arange(N),\n",
    "                   'count': rng.integers(3, 15, size=N),\n",
    "                   'weight': rng.uniform(0, 4, size=N)},\n",
    "                  columns=['basket_id', 'fruit', 'count', 'weight'])\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "11d82c6d-3050-46eb-aab4-cdc23afe064a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0     apple\n",
       "1    orange\n",
       "2     apple\n",
       "3     apple\n",
       "4     apple\n",
       "5    orange\n",
       "6     apple\n",
       "7     apple\n",
       "Name: fruit, dtype: category\n",
       "Categories (2, object): ['apple', 'orange']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将df[\"fruit\"]转为category type\n",
    "fruit_cat = df['fruit'].astype(\"category\")\n",
    "fruit_cat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "aa541a69-fd93-4698-a7fb-c50d329bd1cf",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['apple', 'orange'], dtype='object')"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = fruit_cat.array\n",
    "c.categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "ce3ffeb2-28b5-4924-a0d3-a7730b39bd1b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 0, 0, 0, 1, 0, 0], dtype=int8)"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c.codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "216d35ec-f17c-448c-aba5-2f084660359c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['apple', 'orange', 'apple', 'apple', 'apple', 'orange', 'apple', 'apple']\n",
       "Categories (2, object): ['apple', 'orange']"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.Categorical(df[\"fruit\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "37cdac7e-0c5c-4884-86c4-37ef352c7e2a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['foo', 'bar', 'baz', 'foo', 'bar']\n",
       "Categories (3, object): ['bar', 'baz', 'foo']"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "my_categories = pd.Categorical(['foo', 'bar', 'baz', 'foo', 'bar'])\n",
    "my_categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ebda6730-cbf1-4cc8-b6b1-04ddd3e2dbb7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['foo', 'bar', 'baz', 'foo', 'foo', 'bar']\n",
       "Categories (3, object): ['foo', 'bar', 'baz']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "categories = ['foo', 'bar', 'baz']\n",
    "codes = [0, 1, 2, 0, 0, 1]\n",
    "my_cat2 = pd.Categorical.from_codes(codes, categories)\n",
    "my_cat2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7806bc-ee69-4680-b1cb-7298b1bf8d8f",
   "metadata": {},
   "source": [
    "### 指定categorical有顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "f153f78d-3571-4c8f-a863-ec0376987197",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['foo', 'bar', 'baz', 'foo', 'foo', 'bar']\n",
       "Categories (3, object): ['foo' < 'bar' < 'baz']"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "order_cat = pd.Categorical.from_codes(codes, categories, ordered=True)\n",
    "order_cat"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12ce7807-4017-4091-80f6-f9ce1f47b11f",
   "metadata": {},
   "source": [
    "### pandas.qcut in categorical"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "32dce26b-2186-435c-9213-376796addf8e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.42382504,  1.26372846, -0.87066174, -0.25917323, -0.07534331])"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rng = np.random.default_rng(seed=12345)\n",
    "draws = rng.standard_normal(1000)\n",
    "draws[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6121f593-5ffb-473c-aeee-5a6d8ee9d40b",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[(-3.121, -0.675], (0.687, 3.211], (-3.121, -0.675], (-0.675, 0.0134], (-0.675, 0.0134], ..., (0.0134, 0.687], (0.0134, 0.687], (-0.675, 0.0134], (0.0134, 0.687], (-0.675, 0.0134]]\n",
       "Length: 1000\n",
       "Categories (4, interval[float64, right]): [(-3.121, -0.675] < (-0.675, 0.0134] < (0.0134, 0.687] < (0.687, 3.211]]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins = pd.qcut(draws, 4)\n",
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "ff915229-e898-4622-90b8-d3be76f6beb4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Q1', 'Q4', 'Q1', 'Q2', 'Q2', ..., 'Q3', 'Q3', 'Q2', 'Q3', 'Q2']\n",
       "Length: 1000\n",
       "Categories (4, object): ['Q1' < 'Q2' < 'Q3' < 'Q4']"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins = pd.qcut(draws, 4, labels=[\"Q1\", \"Q2\", \"Q3\", \"Q4\"])\n",
    "bins"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "1363eeb0-9254-427f-b47a-caec8b150ff3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 3, 0, 1, 1, 0, 0, 2, 2, 0], dtype=int8)"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bins.codes[:10]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f64ed3ed-01b0-4b41-ac71-238bae0dac97",
   "metadata": {},
   "source": [
    "### Better performance with categoricals"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "0526f19d-9e87-4117-b410-5c4817dad228",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "N = 10_000_000\n",
    "labels = pd.Series(['foo', 'bar', 'baz', 'qux'] * (N // 4))\n",
    "categorical = labels.astype(\"category\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "866e85a4-2621-4261-9f36-4794f0a7ad87",
   "metadata": {},
   "source": [
    "#### category 使用的内存更少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "e5754e3d-3a3f-41db-a156-58e0c6d0ab6f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "600000128\n",
      "10000540\n"
     ]
    }
   ],
   "source": [
    "print(labels.memory_usage(deep=True))\n",
    "print(categorical.memory_usage(deep=True))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c6073e3-77b7-41da-8005-fb3060aa3650",
   "metadata": {},
   "source": [
    "#### category使用的时间更少"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "3b2791f3-beed-4895-b2a5-06d8f6818272",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 242 ms, sys: 73 ms, total: 315 ms\n",
      "Wall time: 312 ms\n"
     ]
    }
   ],
   "source": [
    "%time _ = labels.astype(\"category\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "10a38a4f-6099-4f8e-a6a8-7bc5386fc486",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "207 ms ± 326 µs per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit labels.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "c99d140d-44cb-4b53-96c3-a5d01bef8fc3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23.1 ms ± 3.65 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit categorical.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d142cdfa-1721-484e-b334-45b34239c7fb",
   "metadata": {},
   "source": [
    "### category中的类Series.str方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "dc26f6a6-e660-4abe-b2b5-548ccf4023a0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    d\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "7    d\n",
       "dtype: category\n",
       "Categories (4, object): ['a', 'b', 'c', 'd']"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = pd.Series(['a', 'b', 'c', 'd'] * 2)\n",
    "cat_s = s.astype(\"category\")\n",
    "cat_s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4cd1743b-b18b-4639-a30a-e6a268021cac",
   "metadata": {},
   "source": [
    "#### cat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "d23970a3-4afb-4297-ab22-31c7f0cb3a26",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    0\n",
       "1    1\n",
       "2    2\n",
       "3    3\n",
       "4    0\n",
       "5    1\n",
       "6    2\n",
       "7    3\n",
       "dtype: int8"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.cat.codes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "1cb04481-4d35-4d85-a92f-15957f7c71ec",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['a', 'b', 'c', 'd'], dtype='object')"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.cat.categories"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "dc858ff1-0052-4314-83e9-8972a8de68a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "2    c\n",
       "3    d\n",
       "4    a\n",
       "5    b\n",
       "6    c\n",
       "7    d\n",
       "dtype: category\n",
       "Categories (5, object): ['a', 'b', 'c', 'd', 'e']"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actual_categories = ['a', 'b', 'c', 'd', 'e']\n",
    "cat_s2 = cat_s.cat.set_categories(actual_categories)\n",
    "cat_s2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "d274127f-794f-49ba-8122-8e8813f0a09f",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    2\n",
       "c    2\n",
       "d    2\n",
       "dtype: int64"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "8aab3c69-61df-457d-9435-036bd311f46c",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a    2\n",
       "b    2\n",
       "c    2\n",
       "d    2\n",
       "e    0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s2.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8bd086f5-7c30-467b-92a9-51dbc1e906f3",
   "metadata": {},
   "source": [
    "#### 删除无用categorial"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "id": "7f81aa0c-7342-4ede-871e-a93d6eeabbd4",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "4    a\n",
       "5    b\n",
       "dtype: category\n",
       "Categories (4, object): ['a', 'b', 'c', 'd']"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s3 = cat_s[cat_s.isin([\"a\",\"b\"])]\n",
    "cat_s3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "4b95bf99-aa5c-45d3-aa11-b0abd931ced7",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    a\n",
       "1    b\n",
       "4    a\n",
       "5    b\n",
       "dtype: category\n",
       "Categories (2, object): ['a', 'b']"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s3.cat.remove_unused_categories()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2724d003-f8dc-4e83-9733-5ba30e09247d",
   "metadata": {},
   "source": [
    "#### 生成独热编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "fe5d7a61-ea89-45a6-8ab3-95ae50ed82a9",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>a</th>\n",
       "      <th>b</th>\n",
       "      <th>c</th>\n",
       "      <th>d</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   a  b  c  d\n",
       "0  1  0  0  0\n",
       "1  0  1  0  0\n",
       "2  0  0  1  0\n",
       "3  0  0  0  1\n",
       "4  1  0  0  0\n",
       "5  0  1  0  0\n",
       "6  0  0  1  0\n",
       "7  0  0  0  1"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cat_s = pd.Series(['a', 'b', 'c', 'd'] * 2, dtype='category')\n",
    "pd.get_dummies(cat_s)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.9"
  },
  "toc-autonumbering": false,
  "toc-showcode": false,
  "toc-showmarkdowntxt": true
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
